package com.tansuo365.test1.service.kucunservice.kucunimpl;

import com.tansuo365.test1.bean.kucunbean.PosunDetails;
import com.tansuo365.test1.bean.kucunbean.WeixiuDetails;
import com.tansuo365.test1.bean.kucunbean.WeixiuZong;
import com.tansuo365.test1.bean.kucunbean.WuliaoKucunRiZhi;
import com.tansuo365.test1.mapper.kucunmapper.WeixiuDetailsMapper;
import com.tansuo365.test1.mapper.kucunmapper.WeixiuZongMapper;
import com.tansuo365.test1.mapper.kucunmapper.WuliaoKucunRiZhiMapper;
import com.tansuo365.test1.mapper.kucunmapper.WuliaoKucunSearMapper;
import com.tansuo365.test1.service.kucunservice.WeixiuZongService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class WeixiuZongServiceImpl implements WeixiuZongService {

    @Resource
    private WeixiuZongMapper weixiuZongMapper;
    @Resource
    private WeixiuDetailsMapper weixiuDetailsMapper;
    @Resource
    private WuliaoKucunSearMapper wuliaoKucunSearMapper;
    @Resource
    private WuliaoKucunRiZhiMapper wuliaoKucunRiZhiMapper;

    @Override
    public String getTodayMaxWeixiuDanNumber() {
        return weixiuZongMapper.getTodayMaxWeixiuDanNumber();
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return weixiuZongMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<WeixiuZong> listWeixiuZong(WeixiuZong weixiuZong) {
        return weixiuZongMapper.listWeixiuZong(weixiuZong);
    }

    @Override
    public int deleteBatchByPKArr(Integer[] ids) {
        return weixiuZongMapper.deleteBatchByPKArr(ids);
    }

    @Override
    public int insertSelective(WeixiuZong weixiuZong) {
        return weixiuZongMapper.insertSelective(weixiuZong);
    }

    @Override
    @Transactional
    public int saveZongAndDetails(WeixiuZong weixiuZong, List<WeixiuDetails> weixiuDetailsList) {

        int test = -100;
        for(WeixiuDetails chukuDetails : weixiuDetailsList){
            //查看物品在库存中是否不够出库数量
            String chukukufang = chukuDetails.getChukukufang();
            String mingchengguige = chukuDetails.getMingchengguige();
            Double chukushuliang = chukuDetails.getChukushuliang();
            Double jieyu = wuliaoKucunRiZhiMapper.checkOutTheNumberEnough(chukukufang, mingchengguige);
            if(chukushuliang>jieyu){
                return test;
            }
            test--;
        }

        int insertCode = 0;
        insertCode = weixiuZongMapper.insertSelective(weixiuZong);
        if (!(insertCode > 0)) {
            return 0;
        }
        Integer zongzhangId = weixiuZong.getId();
        int i = 0;
        int upKu = 0;
        int inSertRizhi = 0;
        int inDetails = 0;
        //保存每个维修单物料
        for (WeixiuDetails weixiuDetails : weixiuDetailsList) {
            //设置对应总表id
            i++;
            weixiuDetails.setZongzhangid(zongzhangId);
            inDetails += weixiuDetailsMapper.insertSelective(weixiuDetails);
//            upKu += wuliaoKucunSearMapper.updateWuliaoPosunWeixiuByWuliaoName(weixiuDetails.getMingchengguige(), weixiuDetails.getChukushuliang(), weixiuDetails.getChukukufang());
            //TODO 将每个保存的详细insert到物料日志表
            //TODO 在保存到物料日志时需要加String type,表明是正常出库/破损出库/维修出库
            WuliaoKucunRiZhi wuliaoKucunRiZhi = new WuliaoKucunRiZhi();
            wuliaoKucunRiZhi.setMingchengguige(weixiuDetails.getMingchengguige());
            wuliaoKucunRiZhi.setXiangmuweihu(weixiuZong.getXiangmumingcheng());
            /**
             * 这里改为了维修数量
             */
            wuliaoKucunRiZhi.setWeixiushuliang(weixiuDetails.getChukushuliang());
            wuliaoKucunRiZhi.setFachuleixing("维修出库");
            wuliaoKucunRiZhi.setDanwei(weixiuDetails.getDanwei());
            wuliaoKucunRiZhi.setRukukufang(weixiuDetails.getChukukufang());
            wuliaoKucunRiZhi.setWuliaobianma(weixiuDetails.getWuliaobianma());
            wuliaoKucunRiZhi.setDalei(weixiuDetails.getDalei());
            inSertRizhi += wuliaoKucunRiZhiMapper.insertSelective(wuliaoKucunRiZhi);
        }
        if (!(i == weixiuDetailsList.size() && i != 0)) {
            return 0;
        } else if (!(i == inDetails)) {
            //update的维修数和条目不一样多,那么就说明有的物料没执行update成功,或者是因为库存没有这个物料
            return -1;
        }
        if (!(inSertRizhi == weixiuDetailsList.size())) {
            return 0;
        }
        return i; //返回的是插入维修详情物料的数量
    }

    @Override
    public WeixiuZong selectByPrimaryKey(Integer id) {
        return weixiuZongMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(WeixiuZong weixiuZong) {
        return weixiuZongMapper.updateByPrimaryKeySelective(weixiuZong);
    }

    @Override
    public int updateZhuangtaiByIds(Integer[] ids) {
        return weixiuZongMapper.updateWXZhuangtaiByIds(ids);
    }
}
